package com.getinstaliker.util;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.instagram.getinstaliker.R;

/**
 * The Class DialogUtil.
 * @author HuanND
 */
public class DialogUtil {

	/**
	 * Show input dialog.
	 *
	 * @param context the context
	 * @param title the title
	 * @param message the message
	 * @param listener the listener
	 */
	public static void showInputDialog(Context context,String title,String message,final DialogListener listener){
		final Dialog dialog = new Dialog(context);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		View vContent = LayoutInflater.from(context).inflate(R.layout.dialog_input_content, null);
		TextView tvTitle = (TextView) vContent.findViewById(R.id.tvTitle);
		TextView tvMessage = (TextView) vContent.findViewById(R.id.tvMessage);
		final EditText edtInput = (EditText)vContent.findViewById(R.id.edtInput);
		Button btnCancel = (Button)vContent.findViewById(R.id.btnCancel);
		Button btnOk = (Button)vContent.findViewById(R.id.btnOK);
		
		tvTitle.setText(title);
		tvMessage.setText(message);
		btnCancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(listener!=null){
					listener.onCancel();
				}
				dialog.dismiss();
			}
		});
		btnOk.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(listener!=null){
					listener.onAccept(edtInput.getText().toString());
				}
				dialog.dismiss();
			}
		});
		dialog.setContentView(vContent);
		dialog.show();
	}
	
	/**
	 * Show dialog.
	 *
	 * @param context the context
	 * @param title the title
	 * @param message the message
	 * @param listener the listener
	 */
	public static void showDialog(Context context,String title,String message,final DialogListener listener){
		final Dialog dialog = new Dialog(context);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		View vContent = LayoutInflater.from(context).inflate(R.layout.dialog_input_content, null);
		TextView tvTitle = (TextView) vContent.findViewById(R.id.tvTitle);
		TextView tvMessage = (TextView) vContent.findViewById(R.id.tvMessage);
		final EditText edtInput = (EditText)vContent.findViewById(R.id.edtInput);
		Button btnCancel = (Button)vContent.findViewById(R.id.btnCancel);
		Button btnOk = (Button)vContent.findViewById(R.id.btnOK);
		
		tvTitle.setText(title);
		tvMessage.setText(message);
		edtInput.setVisibility(View.GONE);
		btnCancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(listener!=null){
					listener.onCancel();
				}
				dialog.dismiss();
			}
		});
		btnOk.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(listener!=null){
					listener.onAccept(null);
				}
				dialog.dismiss();
			}
		});
		dialog.setContentView(vContent);
		dialog.show();
	}
	
	/**
	 * Show confirm dialog.
	 *
	 * @param context the context
	 * @param title the title
	 * @param message the message
	 */
	public static void showConfirmDialog(Context context,String title,String message){
		final Dialog dialog = new Dialog(context);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		View vContent = LayoutInflater.from(context).inflate(R.layout.dialog_input_content, null);
		TextView tvTitle = (TextView) vContent.findViewById(R.id.tvTitle);
		TextView tvMessage = (TextView) vContent.findViewById(R.id.tvMessage);
		final EditText edtInput = (EditText)vContent.findViewById(R.id.edtInput);
		Button btnCancel = (Button)vContent.findViewById(R.id.btnCancel);
		Button btnOk = (Button)vContent.findViewById(R.id.btnOK);
		
		tvTitle.setText(title);
		tvMessage.setText(message);
		edtInput.setVisibility(View.GONE);
		btnCancel.setVisibility(View.GONE);
		btnOk.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.setContentView(vContent);
		dialog.show();
	}
	
	/**
	 * Show confirm dialog.
	 *
	 * @param context the context
	 * @param title the title
	 * @param message the message
	 * @param listener the listener
	 */
	public static void showConfirmDialog(Context context,String title,String message,final DialogListener listener){
		final Dialog dialog = new Dialog(context);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		View vContent = LayoutInflater.from(context).inflate(R.layout.dialog_input_content, null);
		TextView tvTitle = (TextView) vContent.findViewById(R.id.tvTitle);
		TextView tvMessage = (TextView) vContent.findViewById(R.id.tvMessage);
		final EditText edtInput = (EditText)vContent.findViewById(R.id.edtInput);
		Button btnCancel = (Button)vContent.findViewById(R.id.btnCancel);
		Button btnOk = (Button)vContent.findViewById(R.id.btnOK);
		
		tvTitle.setText(title);
		tvMessage.setText(message);
		edtInput.setVisibility(View.GONE);
		btnCancel.setVisibility(View.GONE);
		btnOk.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(listener!=null){
					listener.onAccept(null);
				}
				dialog.dismiss();
			}
		});
		dialog.setContentView(vContent);
		dialog.show();
	}
	
	/**
	 * The listener interface for receiving dialog events.
	 * The class that is interested in processing a dialog
	 * event implements this interface, and the object created
	 * with that class is registered with a component using the
	 * component's <code>addDialogListener<code> method. When
	 * the dialog event occurs, that object's appropriate
	 * method is invoked.
	 *
	 * @see DialogEvent
	 */
	public interface DialogListener{
		
		/**
		 * On cancel.
		 */
		public void onCancel();
		
		/**
		 * On accept.
		 *
		 * @param result the result
		 */
		public void onAccept(String result);
	}
	
	
}
